﻿<!--#include file="includes/database.asp" -->
<!--#include file="includes/global_varaiable.asp" -->
<!--#include file="suatchieu_info.asp" -->
<!--#include file="includes/aspmkrfn.asp" -->


<%
Response.expires = 0
Response.expiresabsolute = Now() - 1
Response.addHeader "pragma", "no-cache"
Response.addHeader "cache-control", "private"
Response.addHeader "cache-control", "no-cache"
Response.addHeader "cache-control", "no-store"
Response.CacheControl = "no-cache"

Response.Buffer = True

' Load key from QueryString
bCopy = True
x_RapID = Request.QueryString("RapID")
If x_RapID = "" Or IsNull(x_RapID) Then
	bCopy = False
End If
x_GiochieuID = Request.QueryString("GiochieuID")
If x_GiochieuID = "" Or IsNull(x_GiochieuID) Then
	bCopy = False
End If
x_Ngaychieu = Request.QueryString("Ngaychieu")
If x_Ngaychieu = "" Or IsNull(x_Ngaychieu) Then
	bCopy = False
End If
x_Gia = Request.QueryString("Gia")
If x_Gia = "" Or IsNull(x_Gia) Then
	bCopy = False
End If

' Get action
sAction = Request.Form("a_add")
If (sAction = "" Or IsNull(sAction)) Then
	If bCopy Then
		sAction = "C" ' Copy record
	Else
		sAction = "I" ' Display blank record
	End If
Else

	' Get fields from form
	x_RapID = Request.Form("x_RapID")
	x_GiochieuID = Request.Form("x_GiochieuID")
	x_FilmId = Request.Form("x_FilmId")
	x_Mota = Request.Form("x_Mota")
	x_Gia = Request.Form("x_Gia")
	x_Ngaychieu = Request.Form("x_Ngaychieu")
End If

Select Case sAction
	Case "C": ' Get a record to display
		If Not LoadData() Then ' Load Record based on key
			Session(ewSessionMessage) = "No records found"
			adoCon.Close ' Close Connection
			Set adoCon = Nothing
			Response.Clear
			Response.Redirect "suatchieu_list.asp"
		End If
	Case "A": ' Add
		If AddData() Then ' Add New Record
			Session(ewSessionMessage) = "Add New Record Successful"
			adoCon.Close ' Close Connection
			Set adoCon = Nothing
			Response.Clear
			Response.Redirect "suatchieu_list.asp"
		Else
		End If
End Select
%>
<!--#include file="header.asp"-->
<script type="text/javascript" src="ew.js"></script>
<script type="text/javascript">
<!--
EW_dateSep = "/"; // set date separator	
//-->
</script>
<script type="text/javascript">
<!--
function EW_checkMyForm(EW_this) {
if (EW_this.x_RapID && !EW_hasValue(EW_this.x_RapID, "SELECT" )) {
	if (!EW_onError(EW_this, EW_this.x_RapID, "SELECT", "Bạn chưa chọn rạp!"))
		return false;
}
if (EW_this.x_GiochieuID && !EW_hasValue(EW_this.x_GiochieuID, "SELECT" )) {
	if (!EW_onError(EW_this, EW_this.x_GiochieuID, "SELECT", "Bạn chưa chọn giờ chiếu"))
		return false;
}

if (EW_this.x_FilmId && !EW_hasValue(EW_this.x_FilmId, "SELECT" )) {
	if (!EW_onError(EW_this, EW_this.x_FilmId, "SELECT", "Bạn chưa chọn phim!"))
		return false; 
}

if (EW_this.x_Gia && !EW_hasValue(EW_this.x_Gia, "SELECT" )) {
	if (!EW_onError(EW_this, EW_this.x_Gia, "SELECT", "Bạn chưa chọn giá vé!"))
		return false; 
}

if (EW_this.x_Ngaychieu && !EW_hasValue(EW_this.x_Ngaychieu, "TEXT" )) {
	if (!EW_onError(EW_this, EW_this.x_Ngaychieu, "TEXT", "Bạn chưa chọn ngày chiếu!"))
		return false;
}
if (!EW_checkusdate(EW_this.x_Ngaychieu.value)) {
	if (!EW_onError(EW_this, EW_this.x_Ngaychieu, "TEXT", "Sai định dạng ngày chiếu (mm/dd/yyyy)"))
		return false; 
}
return true;
}
//-->
</script>

<link rel="stylesheet" type="text/css" media="all" href="calendar/calendar-win2k-1.css" title="win2k-1" />
<script type="text/javascript" src="calendar/calendar.js"></script>
<script type="text/javascript" src="calendar/lang/calendar-en.js"></script>
<script type="text/javascript" src="calendar/calendar-setup.js"></script>
<p><span class="aspmaker">Add to TABLE: SUAT CHIEU<br><br><a href="suatchieu_list.asp">Back to List</a></span></p>
<form name="suatchieu_add" id="suatchieu_add" action="suatchieu_add.asp" method="post" onSubmit="return EW_checkMyForm(this);">
<p>
<input type="hidden" name="a_add" value="A">
<%
If Session(ewSessionMessage) <> "" Then
%>
<p><span class="ewmsg"><%= Session(ewSessionMessage) %></span></p>
<%
	Session(ewSessionMessage) = "" ' Clear message
End If
%>
<table class="ewTable">
	<tr id="r_RapID">
		<td class="ewTableHeader"><span>R&#7841;p ID<span class='ewmsg'>&nbsp;*</span></span></td>
		<td class="ewTableAltRow"><span id="cb_x_RapID">
<%
x_RapIDList = "<select id='x_RapID' name='x_RapID'>"
x_RapIDList = x_RapIDList & "<option value=''>Please Select</option>"
sSqlWrk = "SELECT [RoomID], [RoomName] FROM [PHONGCHIEU]"
Set rswrk = Server.CreateObject("ADODB.Recordset")
rswrk.Open sSqlWrk, adoCon, 1, 2
If Not rswrk.Eof Then
	datawrk = rswrk.GetRows
	rowswrk = UBound(datawrk, 2)
	For rowcntwrk = 0 To rowswrk
		x_RapIDList = x_RapIDList & "<option value='" & datawrk(0, rowcntwrk) & "'"
		If CStr(datawrk(0, rowcntwrk)&"") = CStr(x_RapID&"") Then
			x_RapIDList = x_RapIDList & " selected"
		End If
		x_RapIDList = x_RapIDList & ">" & datawrk(1, rowcntwrk) & "</option>"
	Next
End If
rswrk.Close
Set rswrk = Nothing
x_RapIDList = x_RapIDList & "</select>"
Response.Write x_RapIDList
%>
</span></td>
	</tr>
	<tr id="r_GiochieuID">
		<td class="ewTableHeader"><span>Gi&#7901; chi&#7871;u ID<span class='ewmsg'>&nbsp;*</span></span></td>
		<td class="ewTableAltRow"><span id="cb_x_GiochieuID">
<%
x_GiochieuIDList = "<select id='x_GiochieuID' name='x_GiochieuID'>"
x_GiochieuIDList = x_GiochieuIDList & "<option value=''>Please Select</option>"
sSqlWrk = "SELECT [SessionID], [Description] FROM [GIOCHIEU] order by Description"
Set rswrk = Server.CreateObject("ADODB.Recordset")
rswrk.Open sSqlWrk, adoCon, 1, 2
If Not rswrk.Eof Then
	datawrk = rswrk.GetRows
	rowswrk = UBound(datawrk, 2)
	For rowcntwrk = 0 To rowswrk
		x_GiochieuIDList = x_GiochieuIDList & "<option value='" & datawrk(0, rowcntwrk) & "'"
		If CStr(datawrk(0, rowcntwrk)&"") = CStr(x_GiochieuID&"") Then
			x_GiochieuIDList = x_GiochieuIDList & " selected"
		End If
		x_GiochieuIDList = x_GiochieuIDList & ">" & datawrk(1, rowcntwrk) & "</option>"
	Next
End If
rswrk.Close
Set rswrk = Nothing
x_GiochieuIDList = x_GiochieuIDList & "</select>"
Response.Write x_GiochieuIDList
%>
</span></td>
	</tr>
	<tr id="r_FilmId">
		<td class="ewTableHeader"><span>Film Id<span class='ewmsg'>&nbsp;*</span></span></td>
		<td class="ewTableAltRow"><span id="cb_x_FilmId">
<%
x_FilmIdList = "<select id='x_FilmId' name='x_FilmId'>"
x_FilmIdList = x_FilmIdList & "<option value=''>Please Select</option>"
sSqlWrk = "SELECT [FilmID], [Title] FROM [CHITIETPHIM]"
sSqlWrk = sSqlWrk & " ORDER BY [DateCreated] Desc"
Set rswrk = Server.CreateObject("ADODB.Recordset")
rswrk.Open sSqlWrk, adoCon, 1, 2
If Not rswrk.Eof Then
	datawrk = rswrk.GetRows
	rowswrk = UBound(datawrk, 2)
	For rowcntwrk = 0 To rowswrk
		x_FilmIdList = x_FilmIdList & "<option value='" & datawrk(0, rowcntwrk) & "'"
		If CStr(datawrk(0, rowcntwrk)&"") = CStr(x_FilmId&"") Then
			x_FilmIdList = x_FilmIdList & " selected"
		End If
		x_FilmIdList = x_FilmIdList & ">" & datawrk(1, rowcntwrk) & "</option>"
	Next
End If
rswrk.Close
Set rswrk = Nothing
x_FilmIdList = x_FilmIdList & "</select>"
Response.Write x_FilmIdList
%>
</span></td>
	</tr>
	<tr id="r_Mota">
		<td class="ewTableHeader">Mô t&#7843;</td>
		<td class="ewTableAltRow"><span id="cb_x_Mota">
<textarea id="x_Mota" name="x_Mota" style="width: 224px; height: 69px"><%= x_Mota %></textarea>
</span></td>
	</tr>
	<tr id="r_Gia">
		<td class="ewTableHeader">Giá vé <span class='ewmsg'>&nbsp;*</span></td>
		<td class="ewTableAltRow"><span id="cb_x_Gia">
		
<%
x_FilmIdList = "<select id='x_Gia' name='x_Gia'>"
x_FilmIdList = x_FilmIdList & "<option value=''>Please Select</option>"
sSqlWrk = "SELECT [PriceID], [Price] FROM [GIAVE]"
sSqlWrk = sSqlWrk & " ORDER BY [Price] Asc"
Set rswrk = Server.CreateObject("ADODB.Recordset")
rswrk.Open sSqlWrk, adoCon, 1, 2
If Not rswrk.Eof Then
	datawrk = rswrk.GetRows
	rowswrk = UBound(datawrk, 2)
	For rowcntwrk = 0 To rowswrk
		x_FilmIdList = x_FilmIdList & "<option value='" & datawrk(0, rowcntwrk) & "'"
		If CStr(datawrk(0, rowcntwrk)&"") = CStr(x_FilmId&"") Then
			x_FilmIdList = x_FilmIdList & " selected"
		End If
		x_FilmIdList = x_FilmIdList & ">" & datawrk(1, rowcntwrk) & "</option>"
	Next
End If
rswrk.Close
Set rswrk = Nothing
x_FilmIdList = x_FilmIdList & "</select>"
Response.Write x_FilmIdList
%>
	
</span></td>
	</tr>
	<tr id="r_Ngaychieu">
		<td class="ewTableHeader">Ngày chi&#7871;u<span><span class='ewmsg'>&nbsp;*</span></span></td>
		<td class="ewTableAltRow"><span id="cb_x_Ngaychieu">
<input type="text" name="x_Ngaychieu" id="x_Ngaychieu" value="<% Response.Write x_Ngaychieu %>">
&nbsp;<img src="images/ew_calendar.gif" id="cx_Ngaychieu" alt="Pick a Date" style="cursor:pointer;cursor:hand;">
<script type="text/javascript">
Calendar.setup(
{
inputField : "x_Ngaychieu", // ID of the input field
ifFormat : "%m/%d/%Y", // the date format
button : "cx_Ngaychieu" // ID of the button
}
);
</script>
</span></td>
	</tr>
</table>
<p>
  <input type="submit" name="btnAction" id="btnAction" value="ADD">
</form>
<!--#include file="footer.asp"-->
<%
adoCon.Close ' Close Connection
Set adoCon = Nothing
%>
<%

'-------------------------------------------------------------------------------
' Function LoadData
' - Load Data based on Key Value
' - Variables setup: field variables

Function LoadData()
	
	Dim rs, sSql, sFilter
	sFilter = ewSqlKeyWhere
	sFilter = Replace(sFilter, "@RapID", AdjustSql(x_RapID)) ' Replace key value
	sFilter = Replace(sFilter, "@GiochieuID", AdjustSql(x_GiochieuID)) ' Replace key value
	sFilter = Replace(sFilter, "@Ngaychieu", AdjustSql(x_Ngaychieu)) ' Replace key value
	sSql = ewBuildSql(ewSqlSelect, ewSqlWhere, ewSqlGroupBy, ewSqlHaving, ewSqlOrderBy, sFilter, "")
	Set rs = Server.CreateObject("ADODB.Recordset")
	rs.Open sSql, adoCon
	If rs.Eof Then
		LoadData = False
	Else
		LoadData = True
		rs.MoveFirst

		' Get the field contents
		x_RapID = rs("RoomID")
		x_GiochieuID = rs("SessionID")
		x_FilmId = rs("FilmId")
		x_Mota = rs("Description")
		x_Gia = rs("Price")
		x_Ngaychieu = rs("DayShow")
	End If
	rs.Close
	Set rs = Nothing
End Function
%>
<%

'-------------------------------------------------------------------------------
' Function AddData
' - Add Data
' - Variables used: field variables

Function AddData()
		
	Dim rs, sSql, sFilter
	Dim bCheckKey, sSqlChk, sWhereChk
	sFilter = ewSqlKeyWhere

	' Check for duplicate key
	bCheckKey = True
	If x_RapID = "" Or IsNull(x_RapID) Then
		bCheckKey = False
	Else
		sFilter = Replace(sFilter, "@RapID", AdjustSql(x_RapID)) ' Replace key value
	End If
	If x_GiochieuID = "" Or IsNull(x_GiochieuID) Then
		bCheckKey = False
	Else
		sFilter = Replace(sFilter, "@GiochieuID", AdjustSql(x_GiochieuID)) ' Replace key value
	End If
	If x_Ngaychieu = "" Or IsNull(x_Ngaychieu) Then
		bCheckKey = False
	Else
		sFilter = Replace(sFilter, "@Ngaychieu", AdjustSql(x_Ngaychieu)) ' Replace key value
	End If
	If bCheckKey Then
		sSqlChk = ewBuildSql(ewSqlSelect, ewSqlWhere, ewSqlGroupBy, ewSqlHaving, ewSqlOrderBy, sFilter, "")
		Set rsChk = adoCon.Execute(sSqlChk)
		If Not rsChk.Eof Then
			Session(ewSessionMessage) = "Duplicate value for primary key"
			rsChk.Close
			Set rsChk = Nothing
			AddData = False
			Exit Function
		End If
		rsChk.Close
		Set rsChk = Nothing
	End If

	' Add New Record
	sFilter = "(0 = 1)"
	sSql = ewBuildSql(ewSqlSelect, ewSqlWhere, ewSqlGroupBy, ewSqlHaving, ewSqlOrderBy, sFilter, "")
	Set rs = Server.CreateObject("ADODB.Recordset")
	rs.CursorLocation = 3
	rs.Open sSql, adoCon, 1, 2

	' Clone new rs object
	Dim rsnew
	Set rsnew = rs.Clone(1)
	rs.AddNew

	' Field RapID
	sTmp = x_RapID
	If Not IsNumeric(sTmp) Then
		sTmp = 0
	Else
		sTmp = CLng(sTmp)
	End If
	rs("RoomID") = sTmp

	' Field GiochieuID
	sTmp = x_GiochieuID
	If Not IsNumeric(sTmp) Then
		sTmp = 0
	Else
		sTmp = CLng(sTmp)
	End If
	rs("SessionID") = sTmp

	' Field FilmId
	sTmp = x_FilmId
	If Not IsNumeric(sTmp) Then
		sTmp = Null
	Else
		sTmp = CLng(sTmp)
	End If
	rs("FilmId") = sTmp

	' Field Mota
	sTmp = Trim(x_Mota)
	If Trim(sTmp) = "" Then sTmp = Null
	rs("Description") = sTmp

	' Field Gia
	sTmp = x_Gia
	If Not IsNumeric(sTmp) Then
		sTmp = Null
	Else
		sTmp = cDbl(sTmp)
	End If
	rs("Price") = sTmp

	' Field Ngaychieu
	sTmp = EW_UnFormatDateTime(x_Ngaychieu,2)
	If IsDate(sTmp) Then
		rs("DayShow") = CDate(sTmp)
	Else
		rs("DayShow") = Now
	End If

	' Call recordset inserting event
	If Recordset_Inserting(rs) Then
		rs.Update
		AddData = True
	Else
		rs.CancelUpdate
		AddData = False
	End If
	rs.Close
	Set rs = Nothing

	' Call recordset inserted event
	If AddData Then
	'UserAdd	
		adoCon.Execute "exec AutoAssignSeat " & x_RapID & ", " & "'" & x_Ngaychieu & "'," & x_GiochieuID
	'---end---
		Call Recordset_Inserted(rsnew)
		
	End If
	rsnew.Close
	Set rsnew = Nothing
End Function

'-------------------------------------------------------------------------------
' Recordset inserting event

Function Recordset_Inserting(rsnew)
	On Error Resume Next

	' Please enter your customized codes here
	Recordset_Inserting = True
End Function

'-------------------------------------------------------------------------------
' Recordset inserted event

Sub Recordset_Inserted(rsnew)
	On Error Resume Next

	' Get key value
	Dim sKey
	sKey = ""
	If sKey <> "" Then sKey = sKey & ","
	sKey = sKey & rsnew.Fields("RoomID")
	If sKey <> "" Then sKey = sKey & ","
	sKey = sKey & rsnew.Fields("SessionID")
	If sKey <> "" Then sKey = sKey & ","
	sKey = sKey & rsnew.Fields("DayShow")
End Sub
%>